package likouJava;

public class 搜索插入位置 {

    public static void main(String[] args) {

        int[]array = {1, 5, 8, 11, 19, 22, 31, 35, 40, 45, 48, 49, 50};
        int target = 47;
        int idx = searchInsert(array, target);
        System.out.println(idx);
    }

    public static int searchInsert(int[] nums, int target) {
        int l = 0, r = nums.length - 1, m;

        while (l <= r) {
            m = l  + (r - l) / 2;
            if (nums[m] == target) {
                return m;
            } else if (nums[m] > target) {
                r = m - 1;
            } else {
                l = m + 1;
            }
        }
        return l;
    }

}
